﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.DataBase.Tables.L3INTERFACE
{
    public class L3INTF_TASK_INFO
    {
        #region public property
        public static readonly string tableName = "L3INTF_TASK_INFO";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_taskNo = "TASK_NO";
        public static readonly string fd_pipeQuantityBatch = "PIPE_QUANTITY_BATCH";
        public static readonly string fd_produceDestination = "PRODUCE_DESTINATION";
        public static readonly string fd_priority = "PRIORITY";
        public static readonly string fd_batchSampleRule = "BATCH_SAMPLE_RULE";
        public static readonly string fd_stripSampleRule = "STRIP_SAMPLE_RULE";
        public static readonly string fd_recipeNo = "RECIPE_NO";
        public static readonly string fd_productionStandard = "PRODUCTION_STANDARD";
        public static readonly string fd_pipeDurabilityClass = "PIPE_DURABILITY_CLASS";
        public static readonly string fd_pipeSteelGrade = "PIPE_STEEL_GRADE";
        public static readonly string fd_pipeNominalMass = "PIPE_NOMINAL_MASS";
        public static readonly string fd_pipeNominalLength = "PIPE_NOMINAL_LENGTH";
        public static readonly string fd_pipeMaxLength = "PIPE_MAX_LENGTH";
        public static readonly string fd_pipeMinLength = "PIPE_MIN_LENGTH";
        public static readonly string fd_pipeNominalOd = "PIPE_NOMINAL_OD";
        public static readonly string fd_pipeMaxOd = "PIPE_MAX_OD";
        public static readonly string fd_pipeMinOd = "PIPE_MIN_OD";
        public static readonly string fd_pipeNominalThickness = "PIPE_NOMINAL_THICKNESS";
        public static readonly string fd_pipeMinThickness = "PIPE_MIN_THICKNESS";
        public static readonly string fd_innerBeadIsremoving = "INNER_BEAD_ISREMOVING";
        public static readonly string fd_innerBeadHeight = "INNER_BEAD_HEIGHT";
        public static readonly string fd_innerBeadDepth = "INNER_BEAD_DEPTH";
        public static readonly string fd_ovalityUpLimit = "OVALITY_UP_LIMIT";
        public static readonly string fd_bodyStraightness = "BODY_STRAIGHTNESS";
        public static readonly string fd_endStraightness = "END_STRAIGHTNESS";
        public static readonly string fd_status = "STATUS";
        public static readonly string fd_updateTime = "UPDATE_TIME";

        public DataTable InfcOrderinfo
        {
            get;
            set;
        }
        #endregion
        /// <summary>
        /// L3 generates one task.
        /// </summary>
        /// <param name="event_id"></param>
        /// <returns></returns>
        public static void GeneralTask(string event_id)
        {
            try
            {
                string sqlTask = string.Format("select * from {0} where {1}={2}", tableName, fd_eventNo, event_id);
                DataTable taskTable = OracleHelper.Exct_QueryTable(sqlTask);
                if (taskTable != null && taskTable.Rows.Count == 1)
                {
                    ExecuteGeneralTask(taskTable);
                }
                else
                {
                    //如果一个时间增加了多个
                    string l3infcEventsql = string.Format("update {0} set {1}='{2}',{3}='{4}' where {5}={6}",
                        L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "-1", L3INTF_EVENT.fd_errorInfo, "not unique TASK_NO",
                        L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                    OracleHelper.Exct_NonQuery(l3infcEventsql);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_INFO.GeneralTask", ex);
                throw (ex);
            }
        }
        private static void ExecuteGeneralTask(DataTable taskTable)
        {
            string r = CheckTaskInfoDataVaild(taskTable);
            if (!string.IsNullOrEmpty(r))
            {
                LogHelper.WriteLog("L3INTF_TASK_INFO.ExecuteGeneralTask parameter invalid");
                //update status
                string l3infcEventsql = string.Format("update {0} set {1}='{2}',{3}='{4}' where {5}={6}",
                        L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "-1", L3INTF_EVENT.fd_errorInfo, r,
                        L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                OracleHelper.Exct_NonQuery(l3infcEventsql);
                return;
            }

            try
            {

                string pipeDurabilityClass = " ";
                if (!string.IsNullOrEmpty(taskTable.Rows[0][TASK_INFO.fd_pipeDurabilityClass].ToString()))
                {
                    pipeDurabilityClass = taskTable.Rows[0][TASK_INFO.fd_pipeDurabilityClass].ToString();
                }

                string sqlTask = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}')",
                    TASK_INFO.tableName, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString(), taskTable.Rows[0][TASK_INFO.fd_taskNo].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeQuantityBatch].ToString(), taskTable.Rows[0][TASK_INFO.fd_produceDestination].ToString(), taskTable.Rows[0][TASK_INFO.fd_priority].ToString(), taskTable.Rows[0][TASK_INFO.fd_batchSampleRule].ToString(), taskTable.Rows[0][TASK_INFO.fd_stripSampleRule].ToString(),
                    taskTable.Rows[0][TASK_INFO.fd_recipeNo].ToString(), taskTable.Rows[0][TASK_INFO.fd_productionStandard].ToString(), pipeDurabilityClass, taskTable.Rows[0][TASK_INFO.fd_pipeSteelGrade].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeNominalMass].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeNominalLength].ToString(),
                    taskTable.Rows[0][TASK_INFO.fd_pipeMaxLength].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeMinLength].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeNominalOd].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeMaxOd].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeMinOd].ToString(), taskTable.Rows[0][TASK_INFO.fd_pipeNominalThickness].ToString(),
                    taskTable.Rows[0][TASK_INFO.fd_pipeMinThickness].ToString(), taskTable.Rows[0][TASK_INFO.fd_innerBeadIsremoving].ToString(), taskTable.Rows[0][TASK_INFO.fd_innerBeadHeight].ToString(), taskTable.Rows[0][TASK_INFO.fd_innerBeadDepth].ToString(), taskTable.Rows[0][TASK_INFO.fd_ovalityUpLimit].ToString(), taskTable.Rows[0][TASK_INFO.fd_bodyStraightness].ToString(),
                    taskTable.Rows[0][TASK_INFO.fd_endStraightness].ToString(), taskTable.Rows[0][TASK_INFO.fd_status].ToString(), taskTable.Rows[0][TASK_INFO.fd_updateTime].ToString());
                string l3infcEventsql = string.Format("update {0} set {1}='{2}' where {3}={4}", L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "2", L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                List<string> sqlList = new List<string>();
                sqlList.Add(sqlTask);
                sqlList.Add(l3infcEventsql);
                OracleHelper.ExecuteSqlTran(sqlList);
                LogHelper.WriteLog("L3INTF_TASK_INFO.ExecuteGeneralTask Success!");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_INFO.ExecuteGeneralTask", ex);
                //update status
                string l3infcEventsql = string.Format("update {0} set {1}='{2}',{3}='{4}' where {5}={6}",
                        L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "-1", L3INTF_EVENT.fd_errorInfo, "not unique TASK_NO",
                        L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                OracleHelper.Exct_NonQuery(l3infcEventsql);
                throw ex;
            }
        }
        /// <summary>
        /// L3 updates one task on L2
        /// </summary>
        /// <param name="event_id"></param>
        public static void UpdateTask(string event_id)
        {
            try
            {
                string sqlTask = string.Format("select * from {0} where {1}={2}", tableName, fd_eventNo, event_id);
                DataTable taskTable = OracleHelper.Exct_QueryTable(sqlTask);
                if (taskTable != null && taskTable.Rows.Count == 1)
                {
                    ExecuteUpdateTask(taskTable);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_INFO.UpdateTask", ex);
                throw (ex);
            }
        }
        private static void ExecuteUpdateTask(DataTable taskTable)
        {
            try
            {
                string sqlTask = string.Format("UPDATE {0} SET {1}={2}, {3}='{4}', {5}='{6}',{7}='{8}',{9}='{10}',{11}='{12}',{13}='{14}',{15}='{16}',{17}='{18}',{19}='{20}'" +
                            ",{21}='{22}',{23}='{24}',{25}='{26}',{27}='{28}', {29}='{30}', {31}='{32}',{33}='{34}',{35}='{36}',{37}='{38}',{39}='{40}',{41}='{42}'" +
                            ",{43}='{44}',{45}='{46}',{47}='{48}',{49}='{50}', {51}='{52}', {53}='{54}' where {55}='{56}'",
                               TASK_INFO.tableName, fd_eventNo, taskTable.Rows[0][fd_eventNo].ToString(), fd_pipeQuantityBatch, taskTable.Rows[0][fd_pipeQuantityBatch].ToString(), fd_produceDestination, taskTable.Rows[0][fd_produceDestination].ToString(), fd_priority, taskTable.Rows[0][fd_priority].ToString(),
                               fd_batchSampleRule, taskTable.Rows[0][fd_batchSampleRule].ToString(), fd_stripSampleRule, taskTable.Rows[0][fd_stripSampleRule].ToString(), fd_recipeNo, taskTable.Rows[0][fd_recipeNo].ToString(), fd_productionStandard, taskTable.Rows[0][fd_productionStandard].ToString(), fd_pipeDurabilityClass, taskTable.Rows[0][fd_pipeDurabilityClass].ToString(),
                               fd_pipeSteelGrade, taskTable.Rows[0][fd_pipeSteelGrade].ToString(), fd_pipeNominalMass, taskTable.Rows[0][fd_pipeNominalMass].ToString(), fd_pipeNominalLength, taskTable.Rows[0][fd_pipeNominalLength].ToString(), fd_pipeMaxLength, taskTable.Rows[0][fd_pipeMaxLength].ToString(), fd_pipeMinLength, taskTable.Rows[0][fd_pipeMinLength].ToString(),
                               fd_pipeNominalOd, taskTable.Rows[0][fd_pipeNominalOd].ToString(), fd_pipeMaxOd, taskTable.Rows[0][fd_pipeMaxOd].ToString(), fd_pipeMinOd, taskTable.Rows[0][fd_pipeMinOd].ToString(), fd_pipeNominalThickness, taskTable.Rows[0][fd_pipeNominalThickness].ToString(), fd_pipeMinThickness, taskTable.Rows[0][fd_pipeMinThickness].ToString(),
                               fd_innerBeadIsremoving, taskTable.Rows[0][fd_innerBeadIsremoving].ToString(), fd_innerBeadHeight, taskTable.Rows[0][fd_innerBeadHeight].ToString(), fd_innerBeadDepth, taskTable.Rows[0][fd_innerBeadDepth].ToString(), fd_ovalityUpLimit, taskTable.Rows[0][fd_ovalityUpLimit].ToString(), fd_bodyStraightness, taskTable.Rows[0][fd_bodyStraightness].ToString(),
                               fd_endStraightness, taskTable.Rows[0][fd_endStraightness].ToString(), fd_status, taskTable.Rows[0][fd_status].ToString(), fd_updateTime, taskTable.Rows[0][fd_updateTime].ToString(), fd_taskNo, taskTable.Rows[0][fd_taskNo].ToString());
                string l3infcEventsql = string.Format("update {0} set {1}={2} where {3}={4}", L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "2", L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                List<string> sqlList = new List<string>();
                sqlList.Add(sqlTask);
                sqlList.Add(l3infcEventsql);
                OracleHelper.ExecuteSqlTran(sqlList);
                LogHelper.WriteLog("L3INTF_TASK_INFO.ExecuteUpdateTask Success!");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_INFO.ExecuteUpdateTask", ex);
                throw ex;
            }
        }


        public static string CheckTaskInfoDataVaild(DataTable taskTable)
        {
            if (!int.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeQuantityBatch].ToString(), out int qunaty))
            {
                return "PIPE_QUANTITY_BATCH invalid";
            }
            // need not to check
            //if (string.IsNullOrEmpty(taskTable.Rows[0][TASK_INFO.fd_pipeDurabilityClass].ToString()))
            //{
            //    return "PIPE_DURABILITY_CLASS can not be null";
            //}

            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeNominalMass].ToString(), out double mass))
            {
                return "PIPE_NOMINAL_MASS invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeNominalLength].ToString(), out double nlength))
            {
                return "PIPE_NOMINAL_LENGTH invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeMaxLength].ToString(), out double mxlength))
            {
                return "PIPE_MAX_LENGTH invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeMinLength].ToString(), out double milength))
            {
                return "PIPE_MIN_LENGTH invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeNominalOd].ToString(), out double nod))
            {
                return "PIPE_NOMINAL_OD invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeMaxOd].ToString(), out double mxod))
            {
                return "PIPE_MAX_OD invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeMinOd].ToString(), out double miod))
            {
                return "PIPE_MIN_OD invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeNominalThickness].ToString(), out double nthk))
            {
                return "PIPE_NOMINAL_THICKNESS invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_pipeMinThickness].ToString(), out double mithk))
            {
                return "PIPE_MIN_THICKNESS invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_innerBeadIsremoving].ToString(), out double reming))
            {
                return "INNER_BEAD_ISREMOVING invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_innerBeadHeight].ToString(), out double height))
            {
                return "INNER_BEAD_HEIGHT invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_innerBeadDepth].ToString(), out double depth))
            {
                return "INNER_BEAD_DEPTH invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_ovalityUpLimit].ToString(), out double ovup))
            {
                return "OVALITY_UP_LIMIT invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_bodyStraightness].ToString(), out double bds))
            {
                return "BODY_STRAIGHTNESS invalid";
            }
            if (!double.TryParse(taskTable.Rows[0][TASK_INFO.fd_endStraightness].ToString(), out double eds))
            {
                return "END_STRAIGHTNESS invalid";
            }

            return "";
        }
    }
}
